import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2023-08-13
 * Time: 20:21
 */
public class Test {
    public static void main5(String[] args) {
        //迭代实现斐波那契数列
        for (int i = 0; i < 10; i++) {
            System.out.print(fib2(i) + " ");
        }
    }
    public static int fib2(int n) {
        if(n == 0) {
            return 0;
        }else if(n == 1) {
            return 1;
        }else {
            int f1 = 0;
            int f2 = 1;
            int f3 = 0;
            for (int i = 2; i <=n ; i++) {
                f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
            }
            return f3;
        }
    }
    public static void main4(String[] args) {
        //递归实现斐波那契数列
        for (int i = 0; i < 10; i++) {
            System.out.print(fib(i) + " ");
        }
    }

    public static int fib(int n) {
        if(n == 0) {
            return 0;
        }else if(n == 1) {
            return 1;
        }else {
            return fib(n-1) + fib(n-2);
        }
    }
    public static void main3(String[] args) {
        //写一个递归方法，输入一个非负整数，返回组成它的数字之和. 例如，输入 1729, 则应该返回1+7+2+9，它的和是19

    }

    public static int sum2(int n) {
        if(n < 10) {
            return n;
        }else {
            return sum2(n / 10) + n % 10;
        }
    }
    public static void main2(String[] args) {
        //递归求 1 + 2 + 3 + ... + 10
        int n  = 10;
        System.out.println(sum(n));
    }

    public static int sum(int n) {
        if(n == 1) {
            return 1;
        }else {
            return n + sum(n-1);
        }
    }
    public static void main1(String[] args) {
        //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        print(num);
    }

    public static void print(int num) {
        if(num > 9) {
            print(num/10);
        }
        System.out.println(num % 10);
    }
}
